<!DOCTYPE html>
<html>
<head>
    <title>PlayCanvas Fly Camera</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="icon" type="image/png" href="../playcanvas-favicon.png" />
    <script src="../../build/output/playcanvas.js"></script>
    <style>
        body { 
            margin: 0;
            overflow-y: hidden;
        }
    </style>
</head>

<body>
    <!-- The canvas element -->
    <canvas id="application-canvas"></canvas>

    <!-- The script -->
    <script>
        // ***********    Initialize application   *******************
        var canvas = document.getElementById("application-canvas");

        // Create the application and start the update loop
        var app = new pc.Application(canvas, {
            mouse: new pc.Mouse(canvas),
            keyboard: new pc.Keyboard(window)
        });

        // Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
        app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
        app.setCanvasResolution(pc.RESOLUTION_AUTO);

        window.addEventListener("resize", function () {
            app.resizeCanvas(canvas.width, canvas.height);
        });

        app.scene.ambientLight = new pc.Color(0.2, 0.2, 0.2);

        app.start();

        // ***********    Helper functions    *******************

        function createMaterial (color) {
            var material = new pc.StandardMaterial();
            material.diffuse = color;
            // we need to call material.update when we change its properties
            material.update()
            return material;
        }

        function createBox (position, size, material) {
            // create an entity and add a model component of type 'box'
            var box = new pc.Entity();
            box.addComponent("model", {
                type: "box"
            });

            box.model.material = material;

            // move the box
            box.setLocalPosition(position);
            box.setLocalScale(size);

            // add the box to the hierarchy
            app.root.addChild(box);
        }


        // ***********    Create Boxes    *******************

        // create a few materials for our boxes
        var red = createMaterial(new pc.Color(1,0,0));
        var white = createMaterial(new pc.Color(1,1,1));

        // create a few boxes in our scene
        for (var i=0; i<3; i++) {
            for (var j=0; j<2; j++) {
                createBox(new pc.Vec3(i*2, 0, j*4), pc.Vec3.ONE, red);
            }
        }

        // create a floor
        createBox(new pc.Vec3(0, -0.5, 0), new pc.Vec3(10, 0.1, 10), white);

        // ***********    Create lights   *******************

        // make our scene prettier by adding a directional light
        var light = new pc.Entity();
        light.addComponent("light", {
            type: "point",
            color: new pc.Color(1, 1, 1),
            range: 100
        });
        light.setLocalPosition(0, 0, 2);

        // add the light to the hierarchy
        app.root.addChild(light);

        // ***********    Create camera    *******************

        // Create an Entity with a camera component
        var camera = new pc.Entity();
        camera.addComponent("camera", {
            clearColor: new pc.Color(0.5, 0.5, 0.8),
            nearClip: 0.3,
            farClip: 30
        });

        // add the fly camera script to the camera
        app.assets.loadFromUrl('../assets/scripts/camera/fly-camera.js', 'script', function (err, asset) {
            camera.addComponent("script");
            camera.script.create("flyCamera");
        });

        // add the camera to the hierarchy
        app.root.addChild(camera);

        // Move the camera a little further away
        camera.translate(0, 0, 2);
    </script>
</body>
</html>
